/*
 * File     : internal_platform_PlatformUtilities.c
 * Created  : 28 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "common.h"

/*
 * Class:     com_googlecode_dni_internal_platform_PlatformUtilities
 * Method:    getNativeSizesImpl
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_com_googlecode_dni_internal_platform_PlatformUtilities_getNativeSizesImpl(
    JNIEnv* env,
    jclass clazz,
    jlong pointer )
{
    jint* sizes = (jint*) JLONG_TO_POINTER( pointer );
    sizes[ 0 ] = (jint) sizeof( char );
    sizes[ 1 ] = (jint) sizeof( short );
    sizes[ 2 ] = (jint) sizeof( int );
    sizes[ 3 ] = (jint) sizeof( long );
    sizes[ 4 ] = (jint) sizeof( wchar_t );
    sizes[ 5 ] = (jint) sizeof( size_t );
    sizes[ 6 ] = (jint) sizeof( void* );

    // ISO C allows void*, char*, int*, etc. to be of different sizes.
    // This is weird.  And, crucially, unsupported.
    if ( sizeof( void* ) != sizeof( int* ) )
    {
        (*env)->ThrowNew( env,
                          (*env)->FindClass( env, "java/lang/AssertionError" ),
                          "Unsupported platform; odd pointer sizes" );
    }

    // I guess this is possible on some platforms, but we don't support it.
    if ( sizeof( void* ) != sizeof( size_t ) )
    {
        (*env)->ThrowNew( env,
                          (*env)->FindClass( env, "java/lang/AssertionError" ),
                          "Unsupported platform; differing void* and size_t sizes" );
    }
}
